<template>
	<view :style="colorStyle">
		<view class="top-tabs">
			<view class="left">
				<view class="from">
					<yhdslplace @change="changeFrom">
						<view class="text">{{from}}</view>
					</yhdslplace>
					<image class="arrow-down" src="../../../static/images/arrow_down.png"></image>
				</view>
				<image class="direc" src="../../../static/images/direc_right.png"></image>
				<view class="to">
					<yhdslplace @change="changeTo">
						<view class="text">{{to}}</view>
					</yhdslplace>
					<image class="arrow-down" src="../../../static/images/arrow_down.png"></image>
				</view>
			</view><!--left end-->
			<!-- <view class="tabs" :class="{btborder:type == index}" v-for="(item,index) in tabsList" :key="index"
				@tap="changeTabs(index)">
				{{item.name}}
			</view> -->
		</view>
		<!-- <view class='return-list items'>
			<view class='item' v-for="n in 6" :key="index"
				@click='goOrderDetails(item.id,item.order_no)'>
				<view class="left">
					<image class="avatar" src="../../../static/images/f.png"></image>
				</view>
				<view class="right">
					<view class="title">
						重庆 渝北
						<image class="direc" src="../../../static/images/direc_right.png"></image>
						重庆 垫江
					</view>
					<view class="info">
						一装一卸，今明两天，随时装
					</view>
					<view class="else">
						<view class="e-left">
							<view class="way">
								整车
							</view>
							<view class="user">
								<view class="price">
									运费：
									<text class="p" v-if="data.is_elec==0">¥122.0</text>
									<text class="p" v-if>电议</text>
								</view>
								<view class="name">张老板</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view> -->
		<view class='return-list items' v-if="orderList.length">
			<view class='item' v-for="(item,index) in orderList" :key="index"
				@click='goOrderDetails(item.id,item.order_no)'>
				<view class="left">
					<image class="avatar" src="../../../static/images/f.png"></image>
				</view>
				<view class="right">
					<view class="title">
						{{item.start_city}} {{item.start_distinct}}
						<image class="direc" src="../../../static/images/direc_right.png"></image>
						{{item.end_city}} {{item.end_distinct}}
					</view>
					<view class="info">
						一装一卸，{{item.setup_time}}
					</view>
					<view class="else">
						<view class="e-left">
							<view class="way">
								{{item.wrap_way}}
							</view>
							<view class="user">
								<view class="price">
									运费：
									<text class="p" v-if="item.is_elec==0">¥{{item.self_amount}}</text>
									<text class="p" v-else>电议</text>
								</view>
								<view class="name" v-if="item.start_name && item.start_name.length > 0">{{item.start_name.substring(0,1)}}老板</view>
							</view>
						</view>
						
					</view>
				</view>
			</view>
		</view>
		<view class='loadingicon acea-row row-center-wrapper' v-if="orderList.length > 0">
			<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
		</view>
		<view v-if="orderList.length == 0  && !loading">
			<emptyPage :title="$t(`暂无订单~`)"></emptyPage>
		</view>
		<!-- #ifdef MP -->
		<!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
		<!-- #endif -->
		<!-- #ifndef MP -->
		<home></home>
		<!-- #endif -->
	</view>
</template>

<script>
	import home from '@/components/home';
	import emptyPage from '@/components/emptyPage';
	import {squareRequireList} from '@/api/driver.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import colors from '@/mixins/color.js';
	import yhdslplace from '@/components/yhdsl-place/yhdsl-place.vue';
	
	export default {
		components: {
			home,
			yhdslplace,
			emptyPage,
			// #ifdef MP
			authorize
			// #endif
		},
		mixins: [colors],
		data() {
			return {
				type: 0,
				loading: false,
				loadend: false,
				loadTitle: this.$t(`加载更多`), //提示语
				orderList: [], //订单数组
				orderStatus: -3, //订单状态
				page: 1,
				limit: 10,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				tabsList: [{
					key: 0,
					name: this.$t(`全部`)
				},
				{
					key: 1,
					name: this.$t(`待确认`)
				},
				{
					key: 2,
					name: '运输中'
				}, 
				{
					key: 3,
					name: '待支付'
				}, 
				{
					key: 4,
					name: this.$t(`待评价`)
				}],
				from:'始发地',
				to:'目的地'
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getOrderList();
					}
				},
				deep: true
			}
		},
		mounted() {
			// console.info(options.type)
			// this.type = options.type;
			if (this.isLogin) {
				this.getOrderList();
			} else {
				toLogin();
			}
		},
		onLoad(options) {
			
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			that.page++;
			this.getOrderList();
		},
		onPullDownRefresh() {
			this.page = 1;
			this.getOrderList();
		},
		methods: {
			changeFrom(val) {
				console.log(val);
				this.from = val.data.join(',');
				this.page = 1;
				this.getOrderList();
			},
			changeTo(val) {
				console.log(val);
				this.to = val.data.join(',');
				this.page = 1;
				this.getOrderList(); 
			},
			onLoadFun() {
				this.getOrderList();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			/**
			 * 去订单详情
			 */
			goOrderDetails: function(order_id,order_no) {
				/*
				
				*/
				if (!order_id) return that.$util.Tips({
					title: that.$t(`缺少id无法查看订单详情`)
				});
				uni.navigateTo({
					url: '/pages/driver/square/detail?id=' + order_id
				})
			},
			changeTabs(index) {
				console.info(index)
				this.type = index
				this.loadend = false;
				this.page = 1
				this.limit = 10
				this.orderList = []
				this.getOrderList(index)
			},
			/**
			 * 获取订单列表
			 */
			getOrderList(type) {
				let that = this;
				if (that.loading) return;
				if (that.page != 1 && that.loadend) return;
				uni.showLoading({
					title:'加载中...'
				});
				that.loading = true;
				that.loadTitle = "";
				squareRequireList({
					// type: that.orderStatus,
					page: that.page,
					limit: that.limit,
					from:that.from=="始发地"?'':that.from,
					to:that.to=="目的地"?'':that.to
				}).then(res => {
					uni.hideLoading();
					console.info(res)
					let list = res.data.list || [];
					let loadend = list.length < that.limit;
					that.loadend = loadend;
					that.loading = false;
					that.loadTitle = loadend ? that.$t(`我也是有底线的`) : that.$t(`加载更多`);
					if(that.page == 1){
						that.orderList = list;
					}else{ 
						if(list.length == 0){
							that.page--;
						}
						that.orderList = that.orderList.concat(list);
					}
					that.$set(that, 'orderList', that.orderList);
					
					
				}).catch(err => {
					uni.hideLoading();
					that.loading = false;
					that.loadTitle = that.$t(`加载更多`);
				});
			}
		},
		 //时间戳的处理    
		filters: {
			formatDate: function(value) {
				var date = new Date(value);
				//date.setTime(value);
				var month = date.getMonth() + 1;
				if(month < 10){
					month = "0" + month;
				}
				var hours = date.getHours();
				if (hours < 10)
					hours = "0" + hours;
				var minutes = date.getMinutes();
				if (minutes < 10)
					minutes = "0" + minutes;
				var time = month + "-" + date.getDate() +
					" " + hours + ":" + minutes;
				return time;
			}
 
		},
	}
</script>

<style lang="scss" scoped>
	.items{
		display: flex;
		flex-direction: column;
		.item{
			background-color: white;
			display: flex;
			flex-direction: row;
			padding: 30rpx;
			margin-top: 13rpx;
			border-bottom: 1rpx solid #f1f1f1;
			.left{
				.avatar{
					width:80rpx;
					height:80rpx;
					border-radius: 40rpx;
				}
			}
			.right{
				flex:1;
				margin-left: 30rpx;
				display: flex;
				flex-direction: column;
				.title{
					color: black;
					font-size: 30rpx;
					font-weight: bold;
					display: flex;
					flex-direction: row;
					align-items: center;
					.direc{
						width: 30rpx;
						height:30rpx;
						margin-left: 10rpx;
						margin-right: 10rpx;
					}
				}
				.info{
					color: black;
					font-size: 28rpx;
					margin-top: 10rpx;
				}
				.else{
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;
					.e-left{
						flex:1;
						display: flex;
						flex-direction: column;
						.way{
							color: black;
							font-size: 28rpx;
							margin-top: 10rpx;
						}
						.user{
							margin-top: 10rpx;
							display: flex;
							flex-direction: row;
							justify-content: space-between;
							
							.price{
								color: black;
								font-size: 28rpx;
								.p{
									color: red;
									font-size: 28rpx;
									font-weight: bold;
								}
							}
							.name{
								color: #c1c1c1;
								font-size: 28rpx;
							}
						}
					}
					.e-right{
						width:50rpx;
						height:50rpx;
						.image{
							width:50rpx;
							height:50rpx;
						}
					}
				}
			}
		}
	}
	
	.top-tabs {
		display: flex;
		flex-direction: row;
		align-items: center;
		height: 80rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #f1f1f1;
		.left{
			display: flex;
			flex-direction: row;
			align-items: center;
			.from{
				display: flex;
				flex-direction: row;
				align-items: center;
				.text{
					font-size: 28rpx;
					color: black;
					width: 180rpx;
					text-align: right;
					margin-left: 20rpx;
					white-space: nowrap;
					text-overflow:ellipsis;
					overflow:hidden;
				}
				.arrow-down{
					width: 30rpx;
					height:30rpx;
					margin-left: 10rpx;
				}
			}
			.direc{
				margin-left: 80rpx;
				width: 30rpx;
				height:30rpx;
			}
			.to{
				margin-left: 80rpx;
				display: flex;
				flex-direction: row;
				align-items: center;
				.text{
					font-size: 28rpx;
					color: black;
					max-width: 200rpx;
					white-space: nowrap;
					text-overflow:ellipsis; 
					overflow:hidden;
				}
				.arrow-down{
					width: 30rpx;
					height:30rpx;
					margin-left: 10rpx;
				}
			}
		}
	}

	.top-tabs .tabs {
		position: relative;
		height: 100%;
		padding: 12px 0;
	}

	.btborder {
		&::after {
			position: absolute;
			content: ' ';
			width: 39px;
			height: 2px;
			background-color: var(--view-theme);
			bottom: 2px;
			left: 50%;
			margin-left: -19px;
		}
	}
</style>
